package com.demo.blog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;

/**
 * UploadController
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
public class UploadController extends Controller {
	public void uploadOne() {
	        UploadFile file1 = getFile("file1", PathKit.getWebRootPath() + "/file");
	        File source = file1.getFile();
	        String result = source.getPath() ;
	        setAttr("info",result );
        render("/index/info.html");
    }
	
	public void uploadfile(){}
	
	public void uploadMany() {
	        List<UploadFile> files = getFiles(PathKit.getWebRootPath() + "/file") ;
	        StringBuffer result = new StringBuffer() ; 
	        result.append("list:[") ;
	        for(UploadFile f : files){
	        	 result.append(f.getFile().getPath()).append(" , ").append("\n") ;
	        }
	        result.append("]") ;
       setAttr("info",result.toString() );
       render("/index/info.html"); // 可以以根目录为基础
   }
 
    public void download(){
        String path = getPara(0);
//        String img = PathKit.getWebRootPath() + "/img/u/" + path.replaceAll("_", "/");
//        ZipUtil.zip(img, PathKit.getWebRootPath() + "/img/temp/" + path);
        renderFile("/img/temp/" + path + ".zip");
    }
 
    private String generateWord() {
        String[] beforeShuffle = new String[] { "2", "3", "4", "5", "6", "7",
                "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z" };
        List<String> list = Arrays.asList(beforeShuffle);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String result = afterShuffle.substring(5, 9);
        return result;
    }
}


